load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "explain",
    srcs = [
        "emit.go",
        "explain_factory.go",
        "flags.go",
        "output.go",
        "plan_gist_factory.go",
        "result_columns.go",
        ":gen-explain-factory",  # keep
        ":gen-gist-factory",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/exec/explain",
    visibility = ["//visibility:public"],
    # Pin the dependencies used in auto-generated code.
    deps = [
        "//pkg/geo/geopb",
        "//pkg/kv/kvserver/concurrency/isolation",
        "//pkg/roachpb",
        "//pkg/sql/appstatspb",
        "//pkg/sql/catalog/colinfo",
        "//pkg/sql/catalog/descpb",
        "//pkg/sql/inverted",  # keep
        "//pkg/sql/opt",  # keep
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/constraint",
        "//pkg/sql/opt/exec",
        "//pkg/sql/opt/invertedexpr",  # keep
        "//pkg/sql/pgwire/pgcode",
        "//pkg/sql/pgwire/pgerror",
        "//pkg/sql/sem/catid",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sessiondatapb",
        "//pkg/sql/types",
        "//pkg/util",
        "//pkg/util/errorutil",
        "//pkg/util/humanizeutil",
        "//pkg/util/intsets",
        "//pkg/util/timeutil",
        "//pkg/util/treeprinter",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_dustin_go_humanize//:go-humanize",
    ],
)

go_test(
    name = "explain_test",
    size = "small",
    srcs = [
        "explain_factory_test.go",
        "main_test.go",
        "output_test.go",
        "plan_gist_test.go",
    ],
    data = glob(["testdata/**"]) + [
        "//pkg/sql/opt/testutils/opttester:testfixtures",
    ],
    embed = [":explain"],
    deps = [
        "//pkg/base",
        "//pkg/multitenant/tenantcapabilities",
        "//pkg/security/securityassets",
        "//pkg/security/securitytest",
        "//pkg/server",
        "//pkg/sql/catalog/colinfo",
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/exec",
        "//pkg/sql/opt/memo",
        "//pkg/sql/opt/testutils/opttester",
        "//pkg/sql/opt/testutils/testcat",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/types",
        "//pkg/testutils",
        "//pkg/testutils/datapathutils",
        "//pkg/testutils/serverutils",
        "//pkg/testutils/skip",
        "//pkg/testutils/sqlutils",
        "//pkg/testutils/testcluster",
        "//pkg/util/encoding",
        "//pkg/util/grunning",
        "//pkg/util/leaktest",
        "//pkg/util/log",
        "//pkg/util/treeprinter",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_stretchr_testify//assert",
        "@com_github_stretchr_testify//require",
        "@in_gopkg_yaml_v2//:yaml_v2",
    ],
)

# Define a generator for factory explanation code.
genrule(
    name = "gen-explain-factory",
    srcs = [
        "//pkg/sql/opt:ops",
        "//pkg/sql/opt/exec:defs",
    ],
    outs = ["explain_factory.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/optgen) -out $@ execexplain $(locations //pkg/sql/opt/exec:defs)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/optgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

# Define a generator for gist factory code.
genrule(
    name = "gen-gist-factory",
    srcs = [
        "//pkg/sql/opt:ops",
        "//pkg/sql/opt/exec:defs",
    ],
    outs = ["plan_gist_factory.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/optgen) -out $@ execplangist $(locations //pkg/sql/opt/exec:defs)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/optgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)
